package com.canbot.u05.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.CornerPathEffect;
import android.graphics.Paint;
import android.graphics.Point;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;

import static android.content.ContentValues.TAG;

/**
 * Created by zmp on 2018/1/16.
 */

public class MyControlLayout extends FrameLayout{

        private int measuredWidth;

        private int measuredHeight;

        private int maxR;

        private Paint paint;

        private int outR;

        private int inR;

        private Point centerPoint;

        private int inChildW;

        private GestureDetector mGestureDetector;

        public void setIndexChild(int indexChild) {
                if(indexChild >= 0 && indexChild < getChildCount()) {
                        getChildAt(this.indexChild).setSelected(false);
                        this.indexChild = indexChild;
                        getChildAt(this.indexChild).setSelected(true);
                }
        }

        private int indexChild = 0;


        public MyControlLayout(@NonNull Context context) {
                this(context, null);
        }

        public MyControlLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
                this(context, attrs, 0);
        }

        public MyControlLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
                super(context, attrs, defStyleAttr);
                init();
        }

        private void init() {
                mGestureDetector = new GestureDetector(getContext(), onGestureListener);
                paint = new Paint(Paint.ANTI_ALIAS_FLAG);
                paint.setColor(Color.parseColor("#55A6ACB0"));
                paint.setStyle(Paint.Style.FILL_AND_STROKE);
                CornerPathEffect cornerPathEffect = new CornerPathEffect(200);
                paint.setPathEffect(cornerPathEffect);
        }

        private GestureDetector.OnGestureListener onGestureListener = new GestureDetector.OnGestureListener(){
                @Override
                public boolean onDown(MotionEvent motionEvent) {
                        float x = motionEvent.getX();
                        float y = motionEvent.getY();
                        choiceSelectChild(x, y);
                        return false;
                }

                @Override
                public void onShowPress(MotionEvent motionEvent) {
                        Log.e(TAG, "onShowPress");
                }

                @Override
                public boolean onSingleTapUp(MotionEvent motionEvent) {
                        return false;
                }

                @Override
                public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
                        return false;
                }

                @Override
                public void onLongPress(MotionEvent motionEvent) {
                        Log.e(TAG, "onLongPress");
                }

                @Override
                public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
                        Log.e(TAG, "onFling");
                        return false;
                }
        };

        private void choiceSelectChild(float x, float y) {
                Log.e("TAG", "choiceSelectChild：" + x);
                float v1 = (y - centerPoint.y) * (y - centerPoint.y) + (x - centerPoint.x) * (x - centerPoint.x);
                if(v1 > inR * inR && v1 < outR * outR) {
                        double v = Math.atan2(y - centerPoint.y, x - centerPoint.x);
                        v = Math.PI + v;
                        v = v - Math.PI / 4;
                        if(v <= 0) {
                                v += Math.PI * 2;
                        }
                        for(int i = 0; i < 4; i++) {
                                if(v >= Math.PI / 2 * i && v < Math.PI / 2 * (i + 1)) {
                                        getChildAt(indexChild).setSelected(false);
                                        getChildAt(i).setSelected(true);
                                        indexChild = i;
                                        if(myControlListener != null) {
                                                myControlListener.onDown(indexChild);
                                        }
                                        postDelayed(pressRunnable, 300);
                                        return;
                                }
                        }
                }
        }

        private Runnable pressRunnable = new Runnable(){
                @Override
                public void run() {
                        if(myControlListener != null) {
                                myControlListener.onPress(indexChild);
                        }
                        postDelayed(this, 200);
                }
        };

        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
                super.onSizeChanged(w, h, oldw, oldh);
                measuredWidth = getMeasuredWidth();
                measuredHeight = getMeasuredHeight();
                int maxD = measuredWidth > measuredHeight ? measuredHeight : measuredWidth;
                centerPoint = new Point(measuredWidth / 2, measuredHeight / 2);
                maxR = maxD / 2;
                outR = (int) (maxR * 0.9F);
                inR = (int) (maxR * 0.03F);
                inChildW = (int) (maxR * 0.45F);
        }

        @Override
        protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
                super.onLayout(changed, left, top, right, bottom);

                int width = (int) (outR * 2 * 0.7F);
                int height = (int) (outR);
                getChildAt(0).layout(centerPoint.x - width / 2, centerPoint.y - height - inR, centerPoint.x + width / 2, centerPoint.y - inR);
                getChildAt(2).layout(centerPoint.x - width / 2, centerPoint.y + inR, centerPoint.x + width / 2, centerPoint.y + height + inR);

                getChildAt(3).layout(centerPoint.x - height - inR, centerPoint.y - width / 2, centerPoint.x - inR, centerPoint.y + width / 2);
                getChildAt(1).layout(centerPoint.x + inR, centerPoint.y - width / 2, centerPoint.x + height + inR, centerPoint.y + width / 2);

                getChildAt(4).layout(centerPoint.x - inChildW, centerPoint.y - inChildW, centerPoint.x + inChildW, centerPoint.y + inChildW);


        }

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
                super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }

        @Override
        protected void onDraw(Canvas canvas) {
                super.onDraw(canvas);
                paint.setColor(Color.parseColor("#55A6ACB0"));
                canvas.drawCircle(measuredWidth / 2, measuredHeight / 2, maxR, paint);
        }

        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
                return true;
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
                int action = event.getAction();
                if(action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
                        removeCallbacks(pressRunnable);
                        getChildAt(indexChild).setSelected(false);
                        if(myControlListener != null) {
                                myControlListener.onUp(indexChild, getChildAt(indexChild));
                        }
                }
                mGestureDetector.onTouchEvent(event);
                return true;
        }

        public void setMyControlListener(IMyControlListener mMyControlListener) {
                this.myControlListener = mMyControlListener;
        }

        private IMyControlListener myControlListener;

        public interface IMyControlListener{

                void onDown(int index);

                void onPress(int index);

                void onUp(int index, View childAt);
        }
}
